package view.frontend.order;

import control.OrderControl;
import exceptionManager.ExceptionManager;
import java.awt.Dimension;
import java.awt.event.*;
import java.sql.SQLException;
import java.text.DecimalFormat;
import javax.swing.*;
import model.Telephone;
import model.handlers.OrderHandler;
import view.generaldesign.DesignLayout;
import view.frontend.pdf.*;

/**
 *
 * @author Sonny Sandberg
 */
public class OrderSearchListPanel extends JPanel
{
    private OrderContainer orderContainer;
    private model.Order order;

    public OrderSearchListPanel(final OrderContainer view, model.Order orderObject)
    {
        this.orderContainer = view;
        this.order = orderObject;
        
        setLayout(null);
        setBounds(0, 0, 500, 165);
        setBackground(DesignLayout.T_FIELD_BG_COLOR);
        setBorder(DesignLayout.T_FIELD_RED);
        
        JLabel lblDelete = new JLabel("x");
        lblDelete.setBounds(470, 20, 20, 20);
        lblDelete.setBorder(null);
        lblDelete.setFont(DesignLayout.STANDARD.deriveFont(18f));
        lblDelete.setForeground(DesignLayout.T_FIELD_BORDER_RED);
        lblDelete.addMouseListener(new MouseAdapter() 
        {
            @Override
            public void mouseReleased(MouseEvent evt)
            {
                int confirm = JOptionPane.YES_NO_OPTION;
                int confirmresult = JOptionPane.showConfirmDialog(null, "Ønsker du at slette denne ordre?", "Slet Ordre", confirm);
                if (confirmresult == JOptionPane.YES_OPTION) 
                {
                    try
                    {
                        // Make sure we have the latest order
                        order = OrderHandler.getOrderByID(order.getId());
                        // Det tjekkes igen, at der ikke er modtaget betaling (kunne være sket fra et andet salgssted
                        if (order.getPayment_received() == 0)
                        {
                            boolean deleted = OrderControl.deleteOrder(order);
                            if (deleted == true)
                            {
                                orderContainer.getOrderSearchList().buildExistingScrollPanel("WHERE finished = 0", "ORDER BY orderID DESC", "");
                            }
                        }
                    } 
                    catch (SQLException ex)
                    {
                        ExceptionManager.handleSQLException(ex);
                    } 
                    catch (ClassNotFoundException ex)
                    {
                        ExceptionManager.handleClassNotFoundException(ex);
                    }
                }
            }
        });
        if (order.getPayment_received() == 0 && order.isFinished() == false)
        {
            add(lblDelete);
        }
        
        JLabel lblOrderNumber = new JLabel("<html><b>Ordrenummer:</b> "+order.getId()+"</html>");
        lblOrderNumber.setBounds(10, 10, 520, 20);
        lblOrderNumber.setFont(DesignLayout.STANDARD);
        
        Telephone telephone = order.getCustomerSpecified()[0].getCustomerTelephone();
        
        JLabel lblCustomer = new JLabel("<html><b>Kunde:</b> "+telephone.getNumber()+" ("+telephone.getCustomer().getName()+")</html>");
        lblCustomer.setBounds(10, 30, 520, 20);
        lblCustomer.setFont(DesignLayout.STANDARD);
        
        JLabel lblOrderDate = new JLabel("<html><b>Ordredato:</b> "+order.getOrder_date()+"</html>");
        lblOrderDate.setBounds(10, 50, 520, 20);
        lblOrderDate.setFont(DesignLayout.STANDARD);
        
        DecimalFormat df = new DecimalFormat("#.##");
        JLabel lblSale = new JLabel("<html><b>Totalkøb:</b> "+df.format(order.totalPriceVat())+" inklusiv moms</html>");
        lblSale.setBounds(10, 70, 520, 20);
        lblSale.setFont(DesignLayout.STANDARD);
        
        JLabel lblReceived = new JLabel("<html><b>Beløb Modtaget:</b> "+df.format(order.getPayment_received())+"</html>");
        lblReceived.setBounds(10, 90, 520, 20);
        lblReceived.setFont(DesignLayout.STANDARD);
        
        JPanel panel = new JPanel();
        panel.setOpaque(false);
        panel.setBounds(1, 115, 498, 30);
        
        JButton btnPay = new JButton("bogfør betaling");
        btnPay.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnPay.setBorder(DesignLayout.T_FIELD_RED);
        btnPay.setPreferredSize(new Dimension(100, 25));
        if (order.isFinished() == true)
        {
            btnPay.setEnabled(false);
        }
        btnPay.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                orderContainer.getOrderSearch().getPanel().setVisible(false);
                orderContainer.getOrderSearchList().getPanel().setVisible(false);
                orderContainer.getNewOrderButtonPanel().getPanel().setVisible(false);
                orderContainer.getOrderPayment().buildContentPanel(order);
                orderContainer.getOrderPayment().getPanel().setVisible(true);
            }
        });
        panel.add(btnPay);
        
        JButton btnOrder = new JButton("ordre-/arbejdsseddel");
        btnOrder.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnOrder.setBorder(DesignLayout.T_FIELD_RED);
        btnOrder.setPreferredSize(new Dimension(150, 25));
        btnOrder.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                OrderConfirmation orderConfirmation = new OrderConfirmation(order);
                orderConfirmation.setVisible(true);
            }
        });
        panel.add(btnOrder);
        
        JButton btnInvoice = new JButton("faktura");
        btnInvoice.setPreferredSize(new Dimension(60, 25));
        btnInvoice.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnInvoice.setBorder(DesignLayout.T_FIELD_RED);
        btnInvoice.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (order.getInvoice_date().equals("00.00.0000"))
                {
                    try
                    {
                        int altered = control.OrderControl.addInvoiceDate(order);
                        if (altered > 0)
                        {
                            order = OrderHandler.getOrderByID(order.getId());
                        }
                    } 
                    catch (SQLException ex)
                    {
                        ExceptionManager.handleSQLException(ex);
                    } 
                    catch (ClassNotFoundException ex)
                    {
                        ExceptionManager.handleClassNotFoundException(ex);
                    }
                }
                Invoice invoice = new Invoice(order);
                invoice.setVisible(true);
            }
        });
        panel.add(btnInvoice);
        
        JButton btnCommission = new JButton("provision");
        btnCommission.setPreferredSize(new Dimension(60, 25));
        btnCommission.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnCommission.setBorder(DesignLayout.T_FIELD_RED);
        btnCommission.setBorder(DesignLayout.T_FIELD_RED);
        if (order.getTombStone() == null)
        {
            btnCommission.setEnabled(false);
        }
        btnCommission.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (order.getInvoice_date().equals("00.00.0000"))
                {
                    try
                    {
                        int altered = control.OrderControl.addInvoiceDate(order);
                        if (altered > 0)
                        {
                            order = OrderHandler.getOrderByID(order.getId());
                        }
                    } 
                    catch (SQLException ex)
                    {
                        ExceptionManager.handleSQLException(ex);
                    } 
                    catch (ClassNotFoundException ex)
                    {
                        ExceptionManager.handleClassNotFoundException(ex);
                    }
                }
                Commission commission = new Commission(order);
                commission.setVisible(true);
            }
        });
        panel.add(btnCommission);
        
        JButton btnAccount = new JButton("kontoudtog");
        btnAccount.setPreferredSize(new Dimension(70, 25));
        btnAccount.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        btnAccount.setBorder(DesignLayout.T_FIELD_RED);
        if (order.getTombStone() == null)
        {
            btnAccount.setEnabled(false);
        }
        btnAccount.addActionListener(new ActionListener() 
        {
            @Override
            public void actionPerformed(ActionEvent e)
            {
                if (order.getInvoice_date().equals("00.00.0000"))
                {
                    try
                    {
                        int altered = control.OrderControl.addInvoiceDate(order);
                        if (altered > 0)
                        {
                            order = OrderHandler.getOrderByID(order.getId());
                        }
                    } 
                    catch (SQLException ex)
                    {
                        ExceptionManager.handleSQLException(ex);
                    } 
                    catch (ClassNotFoundException ex)
                    {
                        ExceptionManager.handleClassNotFoundException(ex);
                    }
                }
                AccountStatement accountStatement = new AccountStatement(order);
                accountStatement.setVisible(true);
            }
        });
        panel.add(btnAccount);
        
        add(panel);
        add(lblOrderNumber);
        add(lblCustomer);
        add(lblOrderDate);
        add(lblSale);
        add(lblReceived);
        
        MouseAdapter mouseReleased;
        mouseReleased =
                (
                    new MouseAdapter()
                    {
                        @Override
                        public void mouseReleased(MouseEvent evt)
                        {
                            if (model.BSSingleton.getSingletonObject().getLoggedInUser().getAccessLevel() == 2)
                            {
                                orderContainer.getOrder().showOrder(order);
                                orderContainer.getOrderSearch().getPanel().setVisible(false);
                                orderContainer.getOrderSearchList().getPanel().setVisible(false);
                                orderContainer.getNewOrderButtonPanel().getPanel().setVisible(false);
                                orderContainer.getOrder().getPanel().setVisible(true);
                                orderContainer.getOrder().updateViews();
                            }                            
                        }
                    }
                );
        
        /** Altering an order has been disabled
        if (order.isFinished() == false)
        {
            this.addMouseListener(mouseReleased);
        }
        */
    }
}